home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Sources / Geometry.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  14.4 KB  |  449 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // Geometry.cp 
  3. // Copyright © 1985-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __GEOMETRY__
  7. #include "Geometry.h"
  8. #endif
  9.  
  10. // Toolbox
  11.  
  12. #ifndef __MEMORY__
  13. #include <Memory.h>
  14. #endif
  15.  
  16. #ifndef __TYPES__
  17. #include <Types.h>
  18. #endif
  19.  
  20. // ANSI
  21.  
  22. #ifndef __STDIO__
  23. #include <stdio.h>
  24. #endif
  25.  
  26.  
  27. #pragma segment Main
  28.  
  29. //========================================================================================
  30. // CLASS CPoint
  31. //========================================================================================
  32.  
  33. //----------------------------------------------------------------------------------------
  34. // Arithmatic operators for CPoint. Addition and subtraction are all that make any sense.
  35. // Both the Add and AddTo forms are defined.
  36. //----------------------------------------------------------------------------------------
  37.  
  38.  
  39. //----------------------------------------------------------------------------------------
  40. // CPoint::operator+:
  41. //----------------------------------------------------------------------------------------
  42.  
  43. CPoint CPoint::operator+(const CPoint& pt) const
  44. {
  45.     return CPoint(h + pt.h, v + pt.v);
  46. } // CPoint::operator+
  47.  
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // CPoint::operator-
  51. //----------------------------------------------------------------------------------------
  52.  
  53. CPoint CPoint::operator-(const CPoint& pt) const
  54. {
  55.     return CPoint(h - pt.h, v - pt.v);
  56. } // CPoint::operator-
  57.  
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // CPoint::operator+=:
  61. //----------------------------------------------------------------------------------------
  62.  
  63. CPoint& CPoint::operator+=(const CPoint& pt)
  64. {
  65.     v += pt.v;
  66.     h += pt.h;
  67.     return *this;
  68. } // CPoint::operator+=
  69.  
  70.  
  71. //----------------------------------------------------------------------------------------
  72. // CPoint::operator-=:
  73. //----------------------------------------------------------------------------------------
  74.  
  75. CPoint& CPoint::operator-=(const CPoint& pt)
  76. {
  77.     v -= pt.v;
  78.     h -= pt.h;
  79.     return *this;
  80. } // CPoint::operator-=
  81.  
  82.  
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // Relational operators for CPoint. These are defined by applying the operator in question
  86. // to both coordinates. The condition must hold for both to hold for the Points the
  87. // corresponding Points.
  88. //----------------------------------------------------------------------------------------
  89.  
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // CPoint::operator!=:
  93. //----------------------------------------------------------------------------------------
  94.  
  95. Boolean CPoint::operator!=(const CPoint& pt) const
  96. {
  97.     return v != pt.v || h != pt.h;
  98. } // CPoint::operator!=
  99.  
  100.  
  101.  
  102. //----------------------------------------------------------------------------------------
  103. // CPoint::operator==:
  104. //----------------------------------------------------------------------------------------
  105. Boolean CPoint::operator==(const CPoint& pt) const
  106. {
  107.     return v == pt.v && h == pt.h;
  108. } // CPoint::operator==
  109.  
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // CPoint::operator>:
  113. //----------------------------------------------------------------------------------------
  114.  
  115. Boolean CPoint::operator>(const CPoint& pt) const
  116. {
  117.     return v > pt.v && h > pt.h;
  118. } // CPoint::operator>
  119.  
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // CPoint::operator<:
  123. //----------------------------------------------------------------------------------------
  124.  
  125. Boolean CPoint::operator<(const CPoint& pt) const
  126. {
  127.     return v < pt.v && h < pt.h;
  128. } // CPoint::operator<
  129.  
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // CPoint::operator>=:
  133. //----------------------------------------------------------------------------------------
  134.  
  135. Boolean CPoint::operator>=(const CPoint& pt) const
  136. {
  137.     return v >= pt.v && h >= pt.h;
  138. } // CPoint::operator>=
  139.  
  140.  
  141.  
  142. //----------------------------------------------------------------------------------------
  143. // CPoint::operator<=:
  144. //----------------------------------------------------------------------------------------
  145. Boolean CPoint::operator<=(const CPoint& pt) const
  146. {
  147.     return v <= pt.v && h <= pt.h;
  148. }  // CPoint::operator<=
  149.  
  150.  
  151.  
  152. //========================================================================================
  153. // CLASS CRect
  154. //========================================================================================
  155.  
  156. //----------------------------------------------------------------------------------------
  157. // Operators for adding and subtracting one CRect from to/ from another. Both the Add and
  158. // AddTo form of operators are defined.
  159. //----------------------------------------------------------------------------------------
  160.  
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // CRect::operator+:
  164. //----------------------------------------------------------------------------------------
  165.  
  166. CRect CRect::operator+(const CRect& rt) const
  167. {
  168.     return CRect(left + rt.left, top + rt.top, right + rt.right, bottom + rt.bottom);
  169. } // CRect::operator+
  170.  
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // CRect::operator-:
  174. //----------------------------------------------------------------------------------------
  175.  
  176. CRect CRect::operator-(const CRect& rt) const
  177. {
  178.     return CRect(left - rt.left, top - rt.top, right - rt.right, bottom - rt.bottom);
  179. } // CRect::operator-
  180.  
  181.  
  182. //----------------------------------------------------------------------------------------
  183. // CRect::operator+=:
  184. //----------------------------------------------------------------------------------------
  185.  
  186. CRect& CRect::operator+=(const CRect& rt)
  187. {
  188.     top += rt.top;
  189.     left+= rt.left;
  190.     bottom += rt.bottom;
  191.     right += rt.right;
  192.     
  193.     return *this;
  194. } // CRect::operator+=
  195.  
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // CRect::operator-=:
  199. //----------------------------------------------------------------------------------------
  200.  
  201. CRect& CRect::operator-=(const CRect& rt)
  202. {
  203.     top -= rt.top;
  204.     left-= rt.left;
  205.     bottom-= rt.bottom;
  206.     right-= rt.right;
  207.     
  208.     return *this;
  209. } // CRect::operator-=
  210.  
  211.  
  212.  
  213. //----------------------------------------------------------------------------------------
  214. // Operators for adding and subtracting a CPoint to/ from a CRect. A CPoint is added to a
  215. // CRect by adding the CPoint to both the top-left and bottom-right Points that define the
  216. // CRect. Both the Add and AddTo operators are defined. Very convenient for translating
  217. // Rects. These take a CPoint and since CPoint has a constructor that takes two shorts the
  218. // CRect windowRect can be translated 100 pixels in the positive y direction by the
  219. // statement: 
  220. //
  221. //        windowRect = windowRect + CPoint (0, 100);
  222. // or    windowRect += CPoint (0, 100);
  223. //----------------------------------------------------------------------------------------
  224.  
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // CRect::operator+
  228. //----------------------------------------------------------------------------------------
  229.  
  230. CRect CRect::operator+(const CPoint& pt) const
  231. {
  232.     return CRect(left + pt.h, top + pt.v, right + pt.h, bottom + pt.v);
  233. } // CRect::operator+
  234.  
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // CRect::operator-:
  238. //----------------------------------------------------------------------------------------
  239.  
  240. CRect CRect::operator-(const CPoint& pt) const
  241. {
  242.     return CRect(left - pt.h, top - pt.v, right - pt.h, bottom - pt.v);
  243. } // CRect::operator-
  244.  
  245.  
  246. //----------------------------------------------------------------------------------------
  247. // CRect::operator+=:
  248. //----------------------------------------------------------------------------------------
  249.  
  250. CRect& CRect::operator+=(const CPoint& pt)
  251. {
  252.     top += pt.v;
  253.     bottom+= pt.v;
  254.     left+= pt.h;
  255.     right+= pt.h;
  256.     
  257.     return *this;
  258. } // CRect::operator+=
  259.  
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // CRect::operator-=:
  263. //----------------------------------------------------------------------------------------
  264.  
  265. CRect& CRect::operator-=(const CPoint& pt)
  266. {
  267.     top -= pt.v;
  268.     bottom -= pt.v;
  269.     left -= pt.h;
  270.     right -= pt.h;
  271.     
  272.     return *this;
  273. } // CRect::operator-=
  274.  
  275.  
  276. //----------------------------------------------------------------------------------------
  277. // CRect::Inset:  Inset a CRect using the coordinates in CPoint for the inset delta
  278. //----------------------------------------------------------------------------------------
  279.  
  280. CRect& CRect::Inset(const CPoint& delta)
  281. {
  282.     top += delta.v;
  283.     left += delta.h;
  284.     bottom -= delta.v;
  285.     right -= delta.h;
  286.     
  287.     return *this;
  288. } // CRect::Inset
  289.  
  290.  
  291.  
  292. //----------------------------------------------------------------------------------------
  293. // Equality operators, other relational operator could be defined such as <. But their
  294. // meaning is ambiguous and probably better implemented as methods. For example, aRect <
  295. // bRect could return true if aRect was inside of bRect, or could return true if the area
  296. // of aRect was less than the area of bRect.
  297. //----------------------------------------------------------------------------------------
  298.  
  299.  
  300. //----------------------------------------------------------------------------------------
  301. // CRect::operator==
  302. //----------------------------------------------------------------------------------------
  303.  
  304. Boolean CRect::operator==(const CRect& rt) const
  305. {
  306.     return
  307.         top == rt.top && left == rt.left &&
  308.         bottom == rt.bottom && right == rt.right;
  309. } // CRect::operator==
  310.  
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // CRect::operator!=:
  314. //----------------------------------------------------------------------------------------
  315.  
  316. Boolean CRect::operator!=(const CRect& rt) const
  317. {
  318.     return
  319.         top != rt.top || left != rt.left ||
  320.         bottom != rt.bottom || right != rt.right;
  321. } // CRect::operator!=
  322.  
  323.  
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // Two simple area operators, the macroIntersection & (bitwise and in C++) and the union |
  327. // (bitwise or in C++). The definition of union here is to return a CRect that exactly
  328. // encloses its operands.
  329. //----------------------------------------------------------------------------------------
  330.  
  331.  
  332. //----------------------------------------------------------------------------------------
  333. // CRect::operator&:
  334. //----------------------------------------------------------------------------------------
  335.  
  336. CRect CRect::operator&(const CRect& rt) const
  337. {
  338.     CRect returnRect (Max(left, rt.left), Max(top, rt.top), Min(right, rt.right), Min(bottom, rt.bottom));
  339.     
  340.     if (!returnRect.Valid())
  341.         returnRect.top = returnRect.left = returnRect.bottom = returnRect.right = 0;
  342.     
  343.     return returnRect;
  344. } // CRect::operator&
  345.  
  346.  
  347. //----------------------------------------------------------------------------------------
  348. // CRect::operator|:
  349. //----------------------------------------------------------------------------------------
  350.  
  351. CRect CRect::operator|(const CRect& rt) const
  352. {
  353.     return CRect(Min(left, rt.left), Min(top, rt.top), Max(right, rt.right), Max(bottom, rt.bottom));
  354. } // CRect::operator|
  355.  
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // CRect::Empty: Returns true if the rectangle is empty.
  359. //----------------------------------------------------------------------------------------
  360.  
  361. Boolean CRect::Empty() const
  362. {
  363.     return right <= left || bottom <= top;
  364. } // CRect::Empty
  365.  
  366.  
  367. //----------------------------------------------------------------------------------------
  368. // CRect::Valid: Returns true if a valid rectangle (left < right and top < bottom). If not
  369. // a valid rectangle then returns false.
  370. //----------------------------------------------------------------------------------------
  371.  
  372. Boolean CRect::Valid() const
  373. {
  374.     return left <= right && top <= bottom;
  375. } // CRect::Valid
  376.  
  377.  
  378. //----------------------------------------------------------------------------------------
  379. // CRect::Validate: Fix up coordinates so that left <= right and top <= bottom.
  380. //----------------------------------------------------------------------------------------
  381.  
  382. void CRect::Validate()
  383. {
  384.     if (top > bottom)
  385.     {
  386.         short tmp = top;
  387.         top = bottom;
  388.         bottom = tmp;
  389.     }
  390.     if (left > right)
  391.     {
  392.         short tmp = left;
  393.         left = right;
  394.         right = tmp;
  395.     }
  396. } // CRect::Validate
  397.  
  398.  
  399. //----------------------------------------------------------------------------------------
  400. // CRect::GetLength: Returns the length of a CRect in a given dimension.
  401. //----------------------------------------------------------------------------------------
  402.  
  403. short CRect::GetLength(VHSelect sel) const
  404. {
  405.     return (sel == vSel) ? (bottom - top) : (right - left);
  406. } // CRect::GetLength
  407.  
  408.  
  409. //----------------------------------------------------------------------------------------
  410. // CRect::GetSize: Returns the size of a CRect as a CPoint.
  411. //----------------------------------------------------------------------------------------
  412.  
  413. CPoint CRect::GetSize() const
  414. {
  415.     return CPoint(right - left, bottom - top);
  416. } // CRect::GetSize
  417.  
  418.  
  419. //----------------------------------------------------------------------------------------
  420. // CRect::Contains(CPoint): This method takes a CPoint and returns true if is contained
  421. // within the CRect it is applied to
  422. //----------------------------------------------------------------------------------------
  423.  
  424. Boolean CRect::Contains (const CPoint& pt) const
  425. {
  426.     // Does the CPoint 'pt' lie within the rectangle of 'this'?
  427.     
  428.     return pt.v >= top && pt.v < bottom && pt.h >= left && pt.h < right;
  429. } // CRect::Contains(CPoint)
  430.  
  431.  
  432. //----------------------------------------------------------------------------------------
  433. // CRect::Contains(CRect): This method takes a CRect and returns true if is contained
  434. // within the CRect it is applied to
  435. //----------------------------------------------------------------------------------------
  436.  
  437. Boolean CRect::Contains (const CRect& rt) const
  438. {
  439.     // Does the rectangle 'rt' lie withing the rectagle of 'this'?
  440.     
  441.     return rt.top >= top && rt.bottom <= bottom && rt.left >= left && rt.right <= right;
  442. } // CRect::Contains(CRect)
  443.  
  444.  
  445. //----------------------------------------------------------------------------------------
  446. // End of Geometry.cp
  447.  
  448. #pragma segment Inline
  449.